package cc.buttfu.thingbento;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * @author dislink.now
 * 
 * Android Scrollable lists require an Adapter whose job it is to
 * create and return a View object for every data item in
 * the list. This particular Adapter shows just a single block of text
 * and a thumbnail image for each list item.
 *
 */
/**
 * @author id
 *
 */
public class ShoutListAdapter extends BaseAdapter {

	
	private ArrayList<Post> posts;
	private Context context;
	
	
	/**
	 * Views are created on demand and may be deleted if they scroll off
	 * the list. To avoid having to create them again, they are all stored
	 * in this cache. This might be a bad idea for very long lists of shouts... 
	 */
	private HashMap<Integer, View> cachedViews;
	
	
	/**
	 * Because loading many images from URLs can be very slow, this needs to be
	 * done in a separate thread to avoid stalling the UI.  
	 */
	private ImageLoader imageLoader;
	
	
	/**
	 * This runs forever but waits when the queue becomes empty and needs to
	 * be interrupted to wake up if new items are added to the queue, so we need
	 * to keep a handle on it.
	 */
	private Thread imageLoaderThread;

	/**
	 * @param context
	 * @param posts List of prepared Post items to display in this ListView
	 */
	public ShoutListAdapter(Context context, ArrayList<Post> posts) {
		this.context = context;
		this.posts = posts;
		cachedViews = new HashMap<Integer, View>();
		imageLoader = new ImageLoader(context);
		imageLoaderThread = new Thread( imageLoader );
		imageLoaderThread.start();
	}
	
	@Override
	public int getCount() {
		return posts.size();
	}

	@Override
	public Object getItem(int position) {
		return posts.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		/* TODO: use the convertView parameter to recycle old Views for performance;
		 * when the ShoutPage gains the ability to load multiple pages, this class
		 * should be upgraded so that it no longer stores the views in a cache.
		 */
		
		if( ! cachedViews.containsKey(position) ){
			LinearLayout view = new LinearLayout(context);
			view.setOrientation(LinearLayout.HORIZONTAL);
			
			ImageView img = new ImageView(context);
			img.setMinimumHeight(60);
			img.setMinimumWidth(60);

			imageLoader.queueImage(posts.get(position).getThumbnail(), img);
			
			/* Thread sleeps when queue is empty. Interrupt it to wake it up. */
			imageLoaderThread.interrupt();
			
			/*synchronized(imageLoaderThread){
				imageLoaderThread.notify();
			}*/
			
			/* TODO: Find out how to define this layout in XML in a way that can
			 * be replicated many times for use in a ListView.
			 */
			
			LinearLayout texts = new LinearLayout(context);
			texts.setOrientation(LinearLayout.VERTICAL);
			texts.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

			TextView shoutTv = new TextView(context);
			shoutTv.setText(posts.get(position).getText());
			shoutTv.setTextSize(22);
			shoutTv.setPadding(15, 3, 3, 3);

			TextView userTv = new TextView(context);
			userTv.setText(posts.get(position).getUsername());
			userTv.setTextSize(12);
			userTv.setGravity(Gravity.RIGHT);
			userTv.setLayoutParams( new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT) );
			
			
			texts.addView(shoutTv);
			texts.addView(userTv);
			view.addView(img);

			view.addView(texts);
			
			cachedViews.put(position, view);
		}
		return cachedViews.get(position);
	}

}
